package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.entity.po.DtAuthGroup;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.entity.vo.LocalDtAuthGroupVo;
import com.xueyi.aixihu.entity.vo.SelectVo;
import com.xueyi.aixihu.mapper.LocalDtAuthGroupMapper;
import com.xueyi.aixihu.reqeust.AddAuthGroupReq;
import com.xueyi.aixihu.reqeust.FindAllAuthGroupReq;
import com.xueyi.aixihu.response.FindAllAuthGroupResponse;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.DtAuthGroupService;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lvyi
 * @date : 2022/8/17
 * @Description : 角色表的service实现类
 */
@Service
public class DtAuthGroupServiceImpl implements DtAuthGroupService {

    @Autowired
    private LocalDtAuthGroupMapper localDtAuthGroupMapper;


    /**
     * 查询所有角色名
     *
     * @return 角色名
     */
    @Override
    public BaseResponse<List<SelectVo>> findAllAuthGroupTitle() {
        List<SelectVo> all = localDtAuthGroupMapper.findAllAuthGroupTitle();

        BaseResponse<List<SelectVo>> response = new BaseResponse<>();

        response.setMsg("查询成功");
        response.setCode(SystemCode.SUCCESS);
        response.setResult(all);
        return response;
    }


    /**
     * 查询出所有角色信息 以及关联其角色的条数
     *
     * @return
     */
    @Override
    public FindAllAuthGroupResponse findAllAuthGroup(FindAllAuthGroupReq findAllAuthGroupReq) {

        //如果标题不为空 就将其前后空格去掉
        if (!StringUtils.isBlank(findAllAuthGroupReq.getName())) {
            findAllAuthGroupReq.setName(findAllAuthGroupReq.getName().trim());
        }

        findAllAuthGroupReq.countStartIndex();

        //查询并封装
        List<LocalDtAuthGroupVo> allAuthGroup = localDtAuthGroupMapper.findAllAuthGroup(findAllAuthGroupReq);
        Integer total = localDtAuthGroupMapper.findAllAuthGroupTotal(findAllAuthGroupReq);

        FindAllAuthGroupResponse response = new FindAllAuthGroupResponse();
        response.setMsg("查询成功");
        response.setCode(SystemCode.SUCCESS);
        response.setLocalDtAuthGroupVoList(allAuthGroup);
        response.setTotal(total);
        return response;
    }


    /**
     * 编辑角色的名称
     *
     * @param id   角色id
     * @param name 名称
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> updateAuthName(Integer id, String name) {

        //校验id
        if (id == null || id < 1) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "角色id异常");
        } else if (StringUtils.isBlank(name)) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "角色名称不可为空");
        }

        Integer isNo = localDtAuthGroupMapper.findIsSame(name);
        if (isNo != null && isNo.intValue() != id){
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "该角色名称已被创建", Boolean.FALSE);
        }

        localDtAuthGroupMapper.updateAuthName(id, name.trim());

        LogUtil.buildUserLog(id.toString(), "【角色管理】用户" + DtUsersVo.findCurrentUser().getUsername() + "修改了角色id为：" + id + "的角色名称。", DtUsersVo.findCurrentUser(), null);
        return BaseResponse.build(SystemCode.SUCCESS, "修改成功", Boolean.TRUE);
    }


    /**
     * 修改角色的状态
     *
     * @param id     角色id
     * @param status 角色状态
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> updateAuthStatus(Integer id, Integer status) {

        //校验id
        if (id == null || id < 1) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "角色id异常");
        }

        String title = "";

        if (status == 0) {
            title = "删除";
        } else if (status == 1) {
            title = "恢复";
        }

        localDtAuthGroupMapper.updateAuthStatus(id, status);

        LogUtil.buildUserLog(id.toString(), "【角色管理】用户" + DtUsersVo.findCurrentUser().getUsername() + title + "了角色id为：" + id + "的角色状态。", DtUsersVo.findCurrentUser(), null);
        return BaseResponse.build(SystemCode.SUCCESS, title + "成功", Boolean.TRUE);
    }


    /**
     * 新增角色
     *
     * @param addAuthGroupReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> addAuthGroup(AddAuthGroupReq addAuthGroupReq) {

        //校验名称
        if (StringUtils.isBlank(addAuthGroupReq.getTitle())) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "角色名称不可为空");
        }

        //封装数据
        addAuthGroupReq.setTime(new Date());

        Integer isNo = localDtAuthGroupMapper.findIsSame(addAuthGroupReq.getTitle());
        if (isNo != null){
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "该角色名称已被创建", Boolean.FALSE);
        }

        localDtAuthGroupMapper.addAuthGroup(addAuthGroupReq);
        System.out.println(addAuthGroupReq.getId().toString());
        LogUtil.buildUserLog(addAuthGroupReq.getId().toString(), "【角色管理】用户" + DtUsersVo.findCurrentUser().getUsername() + "新增了id为：" + addAuthGroupReq.getId() + "的角色。", DtUsersVo.findCurrentUser(), null);
        return BaseResponse.build(SystemCode.SUCCESS, "新增成功", Boolean.TRUE);
    }

    /**
     * 根据角色id查询其信息
     *
     * @param id
     * @return
     */
    @Override
    public BaseResponse<DtAuthGroup> findRoleById(Integer id) {
        if (id == null || id < 1) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "角色信息异常", null);
        }

        DtAuthGroup role = localDtAuthGroupMapper.findRoleById(id);
        return BaseResponse.build(SystemCode.SUCCESS, "查询成功", role);
    }
}
